package org.scboy.rbac.loads;

import org.scboy.coobi.sql.Coobi;
import org.scboy.coobi.sql.factory.EntityFactory;
import org.scboy.rbac.dao.RbacOrgDao;
import org.scboy.utils.caches.DataCache;
import org.scboy.utils.caches.rbac.entity.OrgEntity;
import org.scboy.utils.entitys.coobi.Model;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.context.annotation.Configuration;

import cn.hutool.log.Log;
import cn.hutool.log.LogFactory;

/**
 * 组织缓存管理
 */
@Configuration
@EnableCaching
@AutoConfigureAfter(value = {Coobi.class,EntityFactory.class})
public class LoadOrg implements DataCache<OrgEntity> {
	
	Log log =LogFactory.get();

	@Autowired
	EntityFactory factory;
	
	/**
	 * 获取缓存数据
	 * @return
	 */
	@Override
	@Cacheable(value="RBAC_ORG_CACHE",key="#id",unless="#result == null")
	public OrgEntity get(String id) {
		RbacOrgDao dao=factory.getDao(RbacOrgDao.class);
		Model org=dao.get(Model.create().add("id", id).addRc("id").addRc("orgName").addRc("orgLocation").addRc("orgShortName"));
		if(org==null) {
			return null;
		}
		return org.toEntity(OrgEntity.class);
	}
	
	/**
	 * 根据ID更新缓存
	 * @return
	 */
	@Override
	@CachePut(value="RBAC_ORG_CACHE",key = "#result.id",unless="#result == null")
	public OrgEntity update(OrgEntity entity) {
		return entity;
	}
	
	/**
	 * 根据ID更新缓存
	 * @param id
	 * @return
	 */
	@Override
	@CachePut(value="RBAC_ORG_CACHE",key = "#result.id",unless="#result == null")
	public OrgEntity updateByKey(String id) {
		return get(id);
	}
	
	/**
	 * 移除缓存
	 * @param id
	 * @return
	 */
	@Override
	@CacheEvict(value="RBAC_ORG_CACHE",key = "#id")
	public OrgEntity removeCache(String id) {
		return null;
	}
	
	/**
	 * 清空所有缓存
	 * @return
	 */
	@Override
	@CacheEvict(value="RBAC_ORG_CACHE",allEntries = true)
	public void removeAll() {}
}
